<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="标签名称" prop="goodsTag">
        <el-input
          v-model="queryParams.goodsTag"
          placeholder="请输入标签名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleSecend"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="postList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="标签名称" align="center" prop="goodsTag" />

      <el-table-column label="排序" align="center" prop="showSort">
        <template slot-scope="scope">
          {{ scope.row.showSort ? scope.row.showSort : 0 }}
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleSort(scope.row)"
            v-hasPermi="['commodity:tag:changeSort']"
          ></el-button>
        </template>
      </el-table-column>
      <el-table-column label="是否展示" prop="isShow">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isShow"
            :active-value="1"
            :inactive-value="0"
            @change="handleShow(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 修改排序 -->
    <el-dialog title="修改排序" :visible.sync="isSort" width="400px">
      <el-form
        :model="sortForm"
        ref="sort"
        :rules="sortRule"
        label-width="80px"
      >
        <el-form-item label="排序" prop="sort">
          <el-input
            v-model.number="sortForm.showSort"
            placeholder="请输入排序"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormSort">确 定</el-button>
        <el-button @click="isSort = false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 设置场景 -->
    <el-dialog :title="title" :visible.sync="isSecend" width="400px">
      <el-form
        :model="secendForm"
        ref="secend"
        :rules="secendRule"
        label-width="100px"
      >
        <el-form-item label="场景名称" prop="goodsTag">
          <el-input
            v-model="secendForm.goodsTag"
            placeholder="请输入场景名称"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormSecend">确 定</el-button>
        <el-button @click="isSecend = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  tagsList,
  changeSort,
  changeShow,
  tagAdd,
  getMallProductTag,
  updateMallProductTagByPri,
  delDept,
  synProductBrand,
} from "@/api/company/scene";
export default {
  name: "Tags",
  data() {
    return {
      //按钮load
      btnLoad: false,
      // 遮罩层
      loading: true,
      isSecend: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      //是否显示修改排序弹出层
      isSort: false,
      //修改排序
      sortForm: {
        showSort: null,
      },
      secendForm: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      //排序校验
      sortRule: {
        showSort: [
          {
            required: true,
            message: "请输入排序",
            trriger: "blur",
            type: "number",
          },
        ],
      },
      secendRule: {
        goodsTag: [
          {
            required: true,
            message: "请输入场景名称",
            trriger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //同步
    handleSync() {
      this.btnLoad = true;
      synProductBrand()
        .then((res) => {
          this.btnLoad = false;
          this.$modal.msgSuccess("同步成功");
          this.getList();
        })
        .catch((err) => {
          this.btnLoad = false;
        });
    },
    //是否展示
    handleShow(row) {
      const { goodsTagId: productTagId, isShow } = row;

      let text = row.isShow == 1 ? "开启" : "关闭";
      this.$modal
        .confirm("确认要" + text + "展示吗？")
        .then(function () {
          return changeShow({ productTagId, isShow });
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.isShow = row.isShow == 1 ? 0 : 1;
        });
    },
    //修改排序
    handleSort(row) {
      this.isSort = true;
      this.sortForm = {
        showSort: row.showSort ? row.showSort : 0,
        productTagId: row.goodsTagId,
      };
    },

    //修改排序提交
    submitFormSort() {
      this.$refs["sort"].validate((valid) => {
        if (valid) {
          changeSort(this.sortForm).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.isSort = false;
            this.getList();
          });
        }
      });
    },
    reset() {
      this.secendForm = {
        goodsTag: "",
      };
      this.resetForm("secendForm");
    },
    //新增应用场景
    handleSecend(row) {
      console.log(row);

      this.reset();
      // this.secendForm.productBrandId = id;
      this.isSecend = true;
      this.title = "添加应用场景";
    },
    //修改应用场景
    handleUpdate(row) {
      console.log(row);
      let { goodsTagId } = row;
      getMallProductTag(goodsTagId).then((response) => {
        this.secendForm = response.data;
        this.isSecend = true;
        this.title = "修改应用场景";
      });
    },
    //提交应用场景
    submitFormSecend() {
      this.$refs["secend"].validate((valid) => {
        if (valid) {
          console.log(this.secendForm);

          if (this.secendForm.goodsTagId != undefined) {
            updateMallProductTagByPri(this.secendForm).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.isSecend = false;
              this.getList();
            });
          } else {
            tagAdd(this.secendForm).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.isSecend = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除名称为"' + row.goodsTag + '"的数据项？')
        .then(function () {
          return delDept(row.goodsTagId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      tagsList(this.queryParams).then((response) => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.postId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
  },
};
</script>
